<?php 
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use frontend\models\Zhuce;
/**
* 登录
*/
class LoginController extends Controller{
	
	public $layout='mylogin';
	//登录
	public function actionLogin(){
		$model = new Zhuce();
		$request=Yii::$app->request;
		if($request->isPost){
			$post = $request->post('Zhuce');
			//var_dump($post);die;
			$user = $model->find()->where(['username' =>$post['username'],'password' => $post['password']])->one();
			if($user){
				$arr['last_time']=time();
				$arr['last_ip']=Yii::$app->request->userIp;
				$user1=$model->updateAll($arr,'id='.$user->attributes['id']);
				//var_dump($user->attributes);die;
				if($user1){
					$cookie = new \yii\web\Cookie([
		                'name' => 'user',
		                'expire' => time() + 3600,
		                 'httpOnly' => true,
		                 'value' => [$user->attributes['username'],$user->attributes['id'],$user->attributes['last_time'],$user->attributes['last_ip']]
		             ]);
		             Yii::$app->response->getCookies()->add($cookie);
		            return $this->redirect("?r=index/index");
				}				
			}			
		}
		return $this->render('login',['model'=>$model]);
	}
	//注册
	public function actionSignup(){
		$model = new Zhuce();
		$request=Yii::$app->request;
		if($request->isPost){
			$post = $request->post();
			/*$user_ip=$request->userIp;
			$model->creat_ip=$user_ip;*/
			if($model->load($post)){
				//$model->password=md5($post['Zhuce']['password']);
				$model->creat_time=time();
				$model->creat_ip=Yii::$app->request->userIp;
				$res=$model->save(false);
				$id=$model->attributes['id'];
				$result=$model->attributes;
				if($res){
					$cookie = new yii\web\Cookie([
					    'name' => 'user',
					    'expire' => time() + 3600,
					    'httpOnly' => true,
					    'value' => [$result['username'],$result['id'],$result['creat_time'],$result['creat_ip']]
					]);
					Yii::$app->response->getCookies()->add($cookie);
					return $this->redirect("?r=index/index");
				}
			}
		}
		return $this->render('signup',['model'=>$model]);
	}

	//验证码
	public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
	//忘记密码
	public function actionForget(){
		$model = new Zhuce();
		$request = Yii::$app->request;
		if($request->isPost){
			$post=$request->post('Zhuce');
			$user = $model->find()->where(['email' =>$post['email']])->one();
			if($user){
				$mail= Yii::$app->mailer->compose();
				$mail->setTo('1206686483@qq.com');
				$mail->setSubject("邮件测试");  
				//$mail->setTextBody('zheshisha ');   //发布纯文字文本
				$mail->setHtmlBody("修改密码");    //发布可以带html标签的文本
				if($mail->send())  
				    return $this->redirect("?r=login/login");  
				else  
				    echo "失败";   
				die(); 
			}
		}
		return $this->render('forget',['model' => $model]);
	}
	//退出
	public function actionLoginout(){
		$cookie = Yii::$app->request->cookies->get('user');
        //移除一个Cookie对象
        Yii::$app->response->getCookies()->remove($cookie);

        return $this->redirect("?r=login/login");
	}
}



 ?>